using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class admin_UserList : PagedControl
{
    UserDAO daoUser = new UserDAO();
    public override string RequestString
    {
        get {

            if (Request["search"] != null) return "Default.aspx?ctrl=user_list&search=" + Request["search"] + "&page=";
            return "Default.aspx?ctrl=user_list&page=";
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        ActiveAccording = "person";
        p = Request["page"];
        totals = 0;
        currentPage = GetPageNumber(p);
        LoadData();
    }
    public void LoadData()
    {      
        dgrAccount.DataSource = daoUser.GetUserList(currentPage, pageSize, ref totals,Request["search"]==null?"%":("%"+Request["search"]+"%"));       
        dgrAccount.DataBind();
        lblPaging.Text = GetPageGrid(totals, pageSize, currentPage);
    }
    protected void dgrAccount_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try
        {
            int id=int.Parse(dgrAccount.DataKeys[e.RowIndex].Value.ToString());
            UserDAO daoUser = new UserDAO();
            User tmpUser = daoUser.GetUserById(id);
            User user = Session["user"] as User;

            //check if user have enought privilege to delete an account
            //if role value is greater, it means that user have lower privilege
            if  (user.Role > tmpUser.Role  && tmpUser.Id>0)
            {
                ShowMessage("You do not have enough privilege to delete this account.");
                return;
            }
            if (daoUser.Delete(id) > 0)
            {
                ShowMessage("Account is removed");
               LoadData();
            }
            else ShowMessage("Account could not be removed");
        }
        catch (Exception ex)
        {
            ShowMessage("Error. Account could not be removed");
        }
    }
    public string GetRoleName(object role)
    {
        int r = int.Parse(role.ToString());
        if (r == 1) return "Admin";
        else if (r == 1) return "Mng";
        else if (r == 3) return "Staff";
        else if (r == 10) return "Cust";
        else return "";
    }
}
